﻿''' <summary>
''' Classe permettant de convertir un format SAS en un format lisible par OMANE
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class informatChampsBDD
    Inherits PTIC_Util.BaseNotifyPropertyChangeComponentModel

#Region "Variables de classe"

    Private Shared m_ConversionsFormats As List(Of informatChampsBDD)
    Protected Shared Property ConversionsFormats As List(Of informatChampsBDD)
        Get
            If m_ConversionsFormats Is Nothing Then
                MAJ_conversionsPossibles(PTIC_Util.csteEmplacementParametrage & "lstInformat.xml")
            End If
            Return m_ConversionsFormats
        End Get
        Private Set(ByVal value As List(Of informatChampsBDD))
            m_ConversionsFormats = value
        End Set
    End Property

    Protected _formatEntree As FormatChamps
    Protected _formatSortie As FormatChamps
    Protected _nom As String

#End Region

#Region "Constructeurs"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByRef formatEntree As FormatChamps, ByRef formatSortie As FormatChamps)
        _formatEntree = formatEntree
        _formatSortie = formatSortie
        'Recherche de l'informat par défaut entre le champs en entrée et le champs en sortie
        paramInformatDefaut()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New(ByRef formatEntree As FormatChamps, ByRef formatSortie As FormatChamps, ByVal nomInformat As String)
        _formatEntree = formatEntree
        _formatSortie = formatSortie
        'Vérifier que le nom demandé est compatible
        _nom = nomInformat
    End Sub

#End Region

#Region "Propriétés"

    ''' <summary>
    ''' Nom
    ''' </summary>
    ''' <remarks></remarks>
    Public Property nom() As String
        Get
            Return _nom
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("nom")
            _nom = value
            OnPropertyChanged("nom")
        End Set
    End Property

    ''' <summary>
    ''' Format en entrée
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FormatEntree() As FormatChamps
        Get
            Return _formatEntree
        End Get
        Set(ByVal value As FormatChamps)
            OnPropertyChanging("FormatEntree")
            'Paramétrage du format du champs en sortie
            _formatEntree = value
            'Paramétrage de l'informat par défaut pour la conversion en cours
            paramInformatDefaut()
            OnPropertyChanged("FormatEntree")
        End Set
    End Property

    ''' <summary>
    ''' Format en sortie
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property FormatSortie() As FormatChamps
        Get
            Return _formatSortie
        End Get
        Set(ByVal value As FormatChamps)
            OnPropertyChanging("FormatChamps")
            _formatSortie = value
            paramInformatDefaut()
            OnPropertyChanged("FormatChamps")
        End Set
    End Property

    ''' <summary>
    ''' Nom des Informats Possibles
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property NomsInformatPoss() As List(Of String)
        Get
            Dim result As New List(Of String)
            For Each informat As informatChampsBDD In conversionsFormats
                If informat.FormatEntree.estEgal(_formatEntree) And _
                   informat.FormatSortie.estEgal(_formatSortie) Then
                    result.Add(informat.nom)
                End If
            Next
            Return result
        End Get
    End Property

#End Region

#Region "Méthodes de classe"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Function paramInformatDefaut() As Boolean
        'Recherche de l'informat par défaut entre le champs en entrée et le champs en sortie
        Dim lInformatPoss As List(Of String) = NomsInformatPoss()
        Dim result As Boolean = True
        If lInformatPoss.Count = 0 Then
            nom = ""
            MsgBox("Le format source et le format cible paramétrés ne sont pas compatibles")
            result = False
        Else
            nom = lInformatPoss(0)
        End If
        Return result
    End Function


#End Region

#Region "Méthodes statiques - Gestion des possiblités d'association de Formats / informats"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub MAJ_conversionsPossibles(ByVal cheminCompletInformat As String)

        conversionsFormats = New List(Of informatChampsBDD)

        'Chargement du document XML présentant tous les Informat possibles
        Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument
        xmlDoc.Load(cheminCompletInformat)

        'Récupération de la liste des noeuds "INFORMAT"
        Dim lNoeudInformat As Xml.XmlNodeList
        lNoeudInformat = xmlDoc.DocumentElement.GetElementsByTagName("INFORMAT")

        'Pour chaque élément de la liste des noeuds "INFORMAT" : 
        '   - récupération des noeuds "FORMAT_ENTREE", "FORMAT_SORTIE" et "NOM"
        '   - Création de l'informat correspondant et ajout dans la collection d'informat possibles
        Dim noeudInformat, noeudEnf As Xml.XmlNode
        Dim formatEntreeEnCours, formatSortieEnCours As New FormatChamps
        Dim nom As String = ""
        For Each noeudInformat In lNoeudInformat
            For Each noeudEnf In noeudInformat.ChildNodes
                If noeudEnf.Name = "FORMAT_ENTREE" Then
                    Dim test As String = noeudEnf.ChildNodes(0).InnerText.Trim()
                    'Récupération des attributs typeStock et typeAff
                    formatEntreeEnCours = New FormatChamps( _
                        Type.GetType(noeudEnf.ChildNodes(1).InnerText.Trim()), _
                        Type.GetType(noeudEnf.ChildNodes(0).InnerText.Trim()))
                ElseIf noeudEnf.Name = "FORMAT_SORTIE" Then
                    'Récupération des attributs typeStock et typeAff
                    formatSortieEnCours = New FormatChamps( _
                        Type.GetType(noeudEnf.ChildNodes(1).InnerText.Trim()), _
                        Type.GetType(noeudEnf.ChildNodes(0).InnerText.Trim()))
                Else
                    nom = noeudEnf.InnerText.Trim()
                End If
            Next
            conversionsFormats.Add(New informatChampsBDD(formatEntreeEnCours.copy, formatSortieEnCours.copy, String.Copy(nom)))
        Next
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function sontCompatibles(ByVal formatEntree As FormatChamps, ByVal formatSortie As FormatChamps) As Boolean
        Dim result As Boolean = False
        For Each informat As informatChampsBDD In conversionsFormats
            If informat.FormatSortie.estEgal(formatSortie) And _
               informat.FormatEntree.estEgal(formatEntree) Then
                result = True
            End If
        Next
        Return result
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function getFormatSortiePoss(ByVal formatEntree As FormatChamps) As List(Of FormatChamps)
        Dim result As New List(Of FormatChamps)
        For Each informat As informatChampsBDD In conversionsFormats
            If informat.FormatEntree.estEgal(formatEntree) Then
                result.Add(informat.FormatSortie)
            End If
        Next
        Return result
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function getFormatSortieDefaut(ByVal formatEntree As FormatChamps) As FormatChamps
        Return getFormatSortiePoss(formatEntree)(0)
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function getFormatEntreePoss(ByVal formatSortie As FormatChamps) As List(Of FormatChamps)
        Dim result As New List(Of FormatChamps)
        For Each informat As informatChampsBDD In conversionsFormats
            If informat.FormatSortie.estEgal(formatSortie) Then
                result.Add(informat.FormatEntree)
            End If
        Next
        Return result
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function getFormatEntreeDefaut(ByVal formatSortie As FormatChamps) As FormatChamps
        Return getFormatEntreePoss(formatSortie)(0)
    End Function

#End Region

#Region "Méthodes statiques de conversion"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDouble(ByVal sce As Double)
        Return sce
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDouble(ByVal sce As Decimal)
        Return CType(sce, Double)
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDecimal(ByVal sce As Decimal)
        Return sce
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDecimal(ByVal sce As Double)
        Return CType(sce, Decimal)
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDecimal(ByVal sce As String)
        Return CType(sce, Decimal)
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDateTime(ByVal sce As DateTime)
        Return sce
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToDateTime(ByVal sce As Double)
        Return DateAdd(DateInterval.Day, CType(sce, Integer), DateSerial(1960, 1, 1))
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Function convToString(ByVal sce As String)
        Return sce
    End Function

    ''' <summary>
    ''' Renvoie "'" si la variable stockée dans l'outil est de type string.
    ''' "" Sinon
    ''' </summary>
    Public Shared Function idVarString(ByVal t As Type)
        If t.Name = "String" Then
            Return "'"
        Else
            Return ""
        End If
    End Function

#End Region

End Class
